home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / PROGRAMM / OTHER_LA / 3209.ZIP / ENVED.REX < prev    next >
OS/2 REXX Batch file  |  1989-09-21  |  6KB  |  200 lines

  1. /*
  2. ? ENVED.REX is A DOS Environment Editor.  It displays a window with a copy of
  3. ? the master environment.  Standard RXWINDOW editing keys can be used to modify
  4. ? the environment variables.  Modified values are updated in the master
  5. ? environment.  Personal REXX 1.60 or later is required.
  6. ?
  7. ? - To delete environment variables, change the value to blanks.
  8. ? - Variables with blank values are removed from the environment.
  9. ? - ENVED creates a temporary file called "enved.'.  If a TMP=path
  10. ?   environment variable if found, the file is created in that directory.
  11. ?   Otherwise it is created in the current directory.
  12. ? - EXECIO, RXWINDOW, and RXUNLOAD are run.  If a RXDIR=path environment
  13. ?   variable is found, these programs are run from that directory.
  14. ?   Otherwise the default PATH= is used.
  15. ? - Ideas for future enhancements include
  16. ?   - handle RXNEWCOM=YES (probably by creating a temporary BAT file)
  17. ?   - handle very long environment variables and more than 23 variables
  18. ?   - handle blank environment variables
  19. ?   - don't create a temporary file if program stack is installed
  20. ?   - allow editing of variable names (not just the variables)
  21. ?   - make it possible to add an environment variable
  22. ?   - an option to sort the environment variable names
  23.  
  24. On-line help is provided by displaying all lines starting with '?' if any
  25. arguments are specified by the user.
  26.  
  27. Please note changes here:
  28.  
  29. Version History:
  30. 1.0  - Nico Mak, 09/87 - original version
  31. 1.1  - Nico Mak, 10/87 - added logic to insure environment was changed
  32. 1.2  - Charles Daney, 11/87 - use linein instread of execio
  33. */
  34.  
  35. signal on halt
  36. signal on novalue
  37. signal on syntax
  38.  
  39. /* misc initialization and validation */
  40. call initialize arg(1)
  41.  
  42. /* put output from "set" command into array "env.N" */
  43. 'set >' temp_fileid
  44. do i = 1 by 1
  45.     env.i = linein(temp_fileid)
  46.     if env.i = '' then leave
  47.     end
  48. env.0 = i - 1
  49. call dosdel(temp_fileid)
  50. if env.0 > scr_height - 2 then call emsg 'Too many environment variables'
  51.  
  52. /* open window and set border */
  53. start_line = format((scr_height - env.0) / 2, 2, 0)
  54. w = w_open(start_line, 1, env.0 + 2, scr_width)
  55. if w = '' then call emsg 'Error opening a window'
  56. call w_border w, , , , ,color_border
  57. call w_put w, 1, format((scr_width - length(top_header)) / 2, 2, 0), ,
  58.  top_header, , color_prefix
  59. call w_put w, env.0 + 2, ,
  60.  format((scr_width - length(bot_header)) / 2, 2, 0), bot_header, , color_prefix
  61.  
  62. /* setup window with environment variables */
  63. do i = 1 to env.0
  64.    parse var env.i name.i '=' value.i
  65.    call w_put w, i + 1, 2, name.i'=', ,color_prefix
  66.    field_width = scr_width - 3 - length(name.i)
  67.    if length(value.i) > field_width then
  68.       call w_put w, i + 1, 2 + length(name.i) + 1, ,
  69.        ' ... Variable too long for ENVED ...', , color_prefix
  70.    else
  71.       call w_field w, 'value.'i, i + 1, 2 + length(name.i) + 1, ,
  72.        field_width, color_data
  73. end
  74.  
  75. /* make a copy of the original values */
  76. do i = 1 to env.0
  77.    original.i = value.i
  78. end
  79.  
  80. /* display variables and wait for user to finish editing */
  81. call w_read w
  82. call w_close w
  83. w = ''
  84.  
  85. /* update the environment with any changed variables */
  86. changes = 0
  87. do i = 1 to env.0
  88.    if original.i \= value.i then do
  89.       'set' name.i'='value.i
  90.       if dosenv(name.i) \= value.i then
  91.          say 'Unable to set environment variable' name.i
  92.       else
  93.          changes = changes + 1
  94.       end
  95. end
  96. if changes = 0 then say 'Environment unchanged'
  97. else say changes 'environment variable(s) changed'
  98.  
  99. /* unload rxwindow if we loaded it and exit */
  100. call unload
  101. exit 0
  102.  
  103. /* initialize - misc initialization and validation */
  104. initialize:
  105.    /* inititalize w (window number) and rxwindow_loaded */
  106.    w = ''
  107.    rxwindow_loaded = 0
  108.  
  109.    /* display help info if any arguments were specified */
  110.    if arg(1) \= '' then signal explain
  111.  
  112.    /* validate rexx version and rxnewcom setting */
  113.    parse version . v .
  114.    if v < 1.6 then call emsg 'Personal REXX 1.60 or later required'
  115.    if lower(dosenv('rxnewcom')) = 'yes' then
  116.       call emsg 'ENVED does not support RXNEWCOM=YES'
  117.  
  118.    /* set name of temporary fileid and get rxdir setting */
  119.    parse source with . . my_name .
  120.    temp_fileid = envdir('ramdisk')my_name'.tmp'
  121.    rxdir = envdir('rxdir')
  122.  
  123.    /* load rxwindow if necessary */
  124.    if \fcnpkg('rxwindow') then do
  125.       rxdir'rxwindow /q'
  126.       if \fcnpkg('rxwindow') then call emsg 'Error installing RXWINDOW'
  127.       rxwindow_loaded = 1
  128.       end
  129.  
  130.    /* set misc values for window handling */
  131.    scr_height = 25
  132.    scr_width  = 80
  133.    top_header = 'DOS Environment Editor Version 1.1'
  134.    bot_header = 'Press Enter to make all changes, Esc to abort all changes'
  135.    if pcvideo() = 3 then do
  136.        color_prefix = 15
  137.        color_data = 7
  138.        color_border = 7
  139.        end
  140.    else do
  141.        color_prefix = 14
  142.        color_data = 10
  143.        color_border = 6
  144.        end
  145.    return
  146.  
  147.  
  148. /* envdir - get directory name from environment - add \ if necessary */
  149. envdir: procedure
  150.    dir = dosenv(arg(1))
  151.    if dir \= '' then
  152.       if substr(dir, length(dir)) \= '\' then dir = dir'\'
  153.    return dir
  154.  
  155. /* unload rxwindow if we loaded it */
  156. unload:
  157.    if rxwindow_loaded then do
  158.       rxdir'rxunload rxwindow'
  159.       if rc \= 0 then say 'RXUNLOAD RXWINDOW failed - rc' rc
  160.       end
  161.    return
  162.  
  163. /* ctrl-break handler */
  164. halt:
  165.    call emsg 'Ctrl-Break detected - aborting'
  166.  
  167. /* emsg - general purpose fatal error message routine */
  168. emsg:
  169.    if w /= '' then call w_close w
  170.    say arg(1)
  171.    call unload
  172.    exit 99
  173.  
  174. /* explain - when user asks for help */
  175. explain:
  176.    do i = 1 to sourceline()
  177.       parse value sourceline(i) with firstcol +1 rest
  178.       if firstcol = '?' then say rest
  179.    end
  180.    exit
  181.  
  182. /* syntax - error handler based on Wally Pro's syntax routine */
  183. syntax:
  184.    do stax = max(1, sigl - 7) to min(sigl + 3, sourceline())
  185.       call scrwrite , 1, '', 80
  186.       say right(word(stax '===>', 1 + (stax = sigl)), 4) sourceline(stax)
  187.    end
  188.    say '===> REXX error' rc 'in line' sigl':' errortext(rc)
  189.    if queued() > 0 then say queued() 'lines queued. Stackstatus=' stackstatus()
  190.    trace '?r'; nop
  191.    call unload
  192.    exit
  193.  
  194. /* novalue - error handler */
  195. novalue:
  196.    call emsg 'novalue error detected on line' sigl
  197.  
  198.  
  199.  
  200.